package com.atguigu.day09;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.BroadcastState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReadOnlyBroadcastState;
import org.apache.flink.streaming.api.datastream.*;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

//水位超过指定的阈值发送告警，阈值可以动态修改
public class Flink01_OperatorState_BroadcastState {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        //2.从本地端口读取阈值数据
        SingleOutputStreamOperator<Integer> ctrlStream = env.socketTextStream("localhost", 9999).map(r -> Integer.parseInt(r));

        //从hadoop102端口读取水位数据
        SingleOutputStreamOperator<WaterSensor> waterSensorStream = env.socketTextStream("hadoop102", 9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
                    }
                });

        MapStateDescriptor<String, Integer> mapStateDescriptor = new MapStateDescriptor<>("ctrl", String.class, Integer.class);

        //TODO 3.将控制流广播出去
        BroadcastStream<Integer> broadcastStream = ctrlStream.broadcast(mapStateDescriptor);

        //4.将广播流和数据流connect到一起
        BroadcastConnectedStream<WaterSensor, Integer> broadcastConnectedStream = waterSensorStream.connect(broadcastStream);

        broadcastConnectedStream.process(new BroadcastProcessFunction<WaterSensor, Integer, String>() {
            @Override
            public void processElement(WaterSensor value, BroadcastProcessFunction<WaterSensor, Integer, String>.ReadOnlyContext ctx, Collector<String> out) throws Exception {
                //1.先获取到广播状态
                ReadOnlyBroadcastState<String, Integer> broadcastState = ctx.getBroadcastState(mapStateDescriptor);

                //2.获取状态中的数据  如果对方流中没有发数据 那么就给一个默认值0
                int ctrlVc = broadcastState.get("ctrl-vc") == null ? 0 : broadcastState.get("ctrl-vc");

                if (value.getVc() > ctrlVc) {
                    out.collect("报警！！！！！当前水位：" + value.getVc() + "大于给定的阈值：" + ctrlVc);
                }

            }

            @Override
            public void processBroadcastElement(Integer value, BroadcastProcessFunction<WaterSensor, Integer, String>.Context ctx, Collector<String> out) throws Exception {
                //1.提取广播状态
                BroadcastState<String, Integer> broadcastState = ctx.getBroadcastState(mapStateDescriptor);

                //2.将数据放入状态中
                broadcastState.put("ctrl-vc", value);
            }
        }).print();

        env.execute();

    }
}
